<?php

/*
 * 云信发送消息
 */

namespace Yxchat\Controller;

use Common\Controller\ApiController;
use Ruben\Yxyun\Yxim;
use Util\ModelUtils;

class ChatController extends ApiController {
    /*
     * 发送消息 
     */

    public function sendMessage() {
        $input = $this->request();
        $id = $this->user['id'];
        $where['accid'] = $id;
        $where['faccid'] = $input['faccid'];
        $imConfig = C('sdk.yxim');
        $yxim = new Yxim($imConfig);
        $result = $yxim->sendMsg($id, $input['faccid']);
        if ($result['code'] == 200) {
            
        }
        $this->createdResponse();
    }

    /**
     * 创建或者更新群聊信息
     */
    public function storeGroup() {
        $input = $this->request();
        $uid = $this->user['id'];
        $this->checkNull($input, ['group_id']);
        $data = $input;
        $data['owner'] = $uid;
        $result = D('YxGroup')->storeGroup($data);
        if ($result) {
            $this->createdResponse(['code' => 200, 'message' => '创建成功']);
        }
        $this->createdResponse(['code' => 200, 'message' => '创建失败']);
    }

    /*
     * 获取群信息
     */

    public function getGroupinfo() {
        $input = $this->request();
        $this->checkNull($input, ['id']);
        $group_id = $input['id'];
        $uid = $this->user['id'];
        $YxGroup = D('YxGroup')->find($group_id);
        $YxGroup['ismark'] = 0;
        if($group_with_me = D('group_with_me')->where(['group_id'=>$group_id,'user_id'=>$uid,'is_mark'=>1])->find()){
            $YxGroup['ismark'] = 1;
        }
        $this->response($YxGroup);
    }

    /*
     * 获取某人所有群信息
     */

    public function getGroupinfoByUid() {
        $input = $this->request();
        $this->checkNull($input, ['ids']);
        $input['ids'] = explode(',', $input['ids']);
        $where['group_id'] = ['in', $input['ids']];
        $YxGroup = D('YxGroup')->where($where)->select();
        $this->response($YxGroup);
    }
    

    /*
     * 保存群到通讯录，置顶，我的聊名称，是否静音，36小时清除聊天记录 1是，0不是，阅后即焚 1是 0 不是
     */

    public function storeGroupWithMe() {
        $input = $this->request();
        if (!$input['group_id']) {
            $this->errorResponse('4000', '群ID不能为空');
        }
        $group_id = $input['group_id'];
        $group_with_meM = D('group_with_me');
        $uid = $this->user['id'];
        $input['user_id'] = $uid;
        $is_true = $group_with_meM->where(['user_id'=>$uid,'group_id'=>$group_id])->save($input);
        if(!$is_true){
            $group_with_meM->add($input);
        }
        
        $this->createdResponse();
    }
    /*
     * 从通讯录删除群
     */
    public function delGroupWithMe() {
        $input = $this->request();
        $id = $input['group_id'];
        $uid = $this->user['id'];
        $group_with_meM = D('group_with_me');
        $group_with_meM->where(['user_id'=>$uid,'group_id'=>$id])->delete();
        $this->createdResponse();
    }
    
    /*
     * 通讯录列表
     */
    public function listGroupWithMe() {
        $group_with_meM = D('group_with_me');
        $uid = $this->user['id'];
        $group = $group_with_meM->where(['user_id'=>$uid,'is_mark'=>1])->select();
        ModelUtils::hasOne($group, ['foreignKey' => 'group_id', 'relation' => 'YxGroup', 'refKey' => 'group_id','fields'=>['group_id','group_name','icon'],'name' => 'group']);
        $this->response($group);
    }
        
}
